"""
    首先定义卷积批归一化块ConvBNLayer包含卷积层和BatchNorm层
"""
import paddle
import paddle.nn as nn
import paddle.nn.functional as F

class ConvBNLayer(paddle.nn.Layer):
    def __init__(self,num_channels,num_filters,filter_size,stride=1,groups=1,act=None):
        """
        :param num_channels: 卷积层的输入通道数
        :param num_filters: 卷积层的输出通道数
        :param filter_size:
        :param stride: 卷积层的步副
        :param groups: 分组卷积的组数，默认group=1不使用分组卷积
        :param act:
        """
        super(ConvBNLayer,self).__init__()

        # 创建卷积层
        self.conv = nn.Conv2D(in_channels=num_channels,
                              out_channels=num_filters,
                              kernel_size=filter_size,
                              stride=stride,
                              padding=(filter_size -1) //2,
                              groups=groups,
                              bias_attr=False)

        # 创建BatchNorm层
        self._batch_norm = paddle.nn.BatchNorm2D(num_filters)

        self.act = act

    def forward(self,inputs):
        y = self.conv(inputs)
        y = self._batch_norm(y)
        if self.act == 'leaky':
            y = F.leaky_relu(x=y,negative_slope=0.1)
        elif self.act == 'relu':
            y = F.relu(x=y)

        return y